package com.linkallcloud.web.controller;

import java.time.Duration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linkallcloud.core.dto.Trace;
import com.linkallcloud.core.dto.Tree;
import com.linkallcloud.core.exception.BizException;
import com.linkallcloud.core.face.message.request.ObjectFaceRequest;
import com.linkallcloud.core.log.Log;
import com.linkallcloud.core.log.Logs;
import com.linkallcloud.core.security.Pwds;
import com.linkallcloud.core.www.ISimpleUser;
import com.linkallcloud.core.www.utils.WebUtils;
import com.linkallcloud.web.face.annotation.Face;
import com.linkallcloud.web.utils.Controllers;
import com.linkallcloud.web.utils.IOnlineHelper;
import com.linkallcloud.web.vc.ReidsValidateCode;

public abstract class AbstractLocalLoginController {
    protected final Log log = Logs.get();

    protected abstract IOnlineHelper getOnlineHelper();

    protected abstract RedisTemplate<String, String> getRedisTemplate();

    protected String localLoginAndGenToken(ISimpleUser psu, Duration timeout) {
        if (psu != null) {
            return getOnlineHelper().login(psu, timeout);
        }
        throw new BizException("e.login", "登陆失败");
    }

    @Face(simple = true)
    @RequestMapping(value = "/getLoginUser", method = RequestMethod.POST)
    public @ResponseBody Object getLoginUser(ObjectFaceRequest<String> fr, Trace t, ISimpleUser suser) {
        return doGetLoginUser(t, suser);
    }

    protected ISimpleUser doGetLoginUser(Trace t, ISimpleUser suser) {
        suser.setNonce(Pwds.me().getPublicKey());
        return suser;
    }

    @Face(simple = true, login = false)
    @RequestMapping(value = "/exit", method = RequestMethod.POST)
    public @ResponseBody Object exit(ObjectFaceRequest<Object> fr, HttpServletRequest request,
            HttpServletResponse response) {
        return doExit(fr, request, response);
    }

    private Object doExit(ObjectFaceRequest<Object> fr, HttpServletRequest request, HttpServletResponse response) {
        getOnlineHelper().logout(request);
        Controllers.removeLacTokenCookie(request, response);
        return true;
    }

    @Face(simple = true, login = false)
    @RequestMapping(value = "/pub/nonce", method = RequestMethod.POST)
    public @ResponseBody Object nonce(ObjectFaceRequest<String> fr, Trace t) {
        return Pwds.me().getPublicKey();
    }

    @RequestMapping(value = "/verifyCode", method = RequestMethod.GET)
    public void genVerify(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        ReidsValidateCode svc = new ReidsValidateCode(getRedisTemplate(), true);
        svc.generate(request, response);
    }

    protected Integer parseAppClazz(ISimpleUser user, HttpServletRequest request, HttpServletResponse response) {
        Integer clazz = null;
        if (user != null) {
            if ("Yw".equals(user.getUserType())) {
                clazz = 0;
            } else {
                clazz = 1;
            }
        } else {
            clazz = WebUtils.getAppClazzFromCookie(request);
        }
        return clazz;
    }

    protected void ignoreMenuButtons(List<Tree> menuItems) {
        if (menuItems != null && menuItems.size() > 0) {
            int size = menuItems.size();
            for (int i = size - 1; i >= 0; i--) {
                Tree item = menuItems.get(i);
                if (item.getType().equals("1")) {
                    menuItems.remove(item);
                } else {
                    if (item.getChildren() != null && item.getChildren().size() > 0) {
                        ignoreMenuButtons(item.getChildren());
                    }
                }
            }
        }
    }

}
